package com.piece.core.framework.util.security.coder;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

/**
 * SHA加密方法
 */
public class SHACoder {

    /**
     * SHA-1加密
     */
    public static byte[] encodeSHA(String data) throws Exception {
        return digest("SHA", data);
    }

    /**
     * SHA-1加密
     */
    public static byte[] encodeSHA1(String data) throws Exception {
        return digest("SHA-1", data);
    }

    /**
     * SHA-256加密
     */
    public static byte[] encodeSHA256(String data) throws Exception {
        return digest("SHA-256", data);
    }

    /**
     * SHA-384加密
     */
    public static byte[] encodeSHA384(String data) throws Exception {
        return digest("SHA-384", data);
    }

    /**
     * SHA-512加密
     */
    public static byte[] encodeSHA512(String data) throws Exception {
        return digest("SHA-512", data);
    }

    /**
     * SHA-224加密
     */
    public static byte[] encodeSHA224(String data) throws Exception {
        return digest("SHA-224", data);
    }

    private static byte[] digest(String algorithm, String data) throws Exception {
        // 初始化MessageDigest
        MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
        // 执行消息摘要
        return messageDigest.digest(data.getBytes(StandardCharsets.UTF_8));
    }
}